package com.jielihaofeng.consumerspringboot;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Johnnie Wind
 * @description
 * @date 2021/11/30 10:40
 */
@Component
public class RabbitMQListener {

    @RabbitListener(queues = "boot_queue")
    public void listenerQueue(Message message, Channel channel) throws InterruptedException, IOException {
        Thread.sleep(1000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //1.接收转换消息
            System.out.println(new String(message.getBody()));

            //2.处理业务逻辑
            System.out.println("处理业务逻辑...");

            // 测试异常情况
            int i = 3/0;//出现错误

            //3.手动签收
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {

            //4.拒绝签收
            /*
             * 第二个参数：multiple
             * 第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
             *
             * */
            channel.basicNack(deliveryTag, true, true);
            //channel.basicReject(deliveryTag,true);只允许单挑确认
        }
    }
}
